﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Text;
using System.Data;

public partial class Evaluate_AllReport : System.Web.UI.Page
{
    private decimal TotalScore;
    private decimal TaskScore;
    private decimal BaseScore;
    private decimal InspectScore;
    private decimal CheckScore;
    private decimal AwardScore;
    private List<ResultInfo> results = new List<ResultInfo>();
    private string ConstDepartmentID = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "320601"))
        {
            ButtonStandard.Visible = false;
        }
        ConstDepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!IsPostBack)
        {
            BindYearList();
            BindStartMonthList();
            BindEndMonthList();
            CreateTable();
        }
    }

    private string GetWhere(string DepartmentID2)
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        DepartmentID = DepartmentID2.Substring(0, 4) + DepartmentID.Substring(4, 2) + "0";
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            ReturnStr = "And SubString(DepartmentID2,6,1) IN('1','2','3','4') And  SubString(DepartmentID2,5,1) ='0' And Left(DepartmentID2,4)='" + DepartmentID.Substring(0, 4) + "'";
        }
        else
        {
            //总队人员
            if (DepartmentID.Substring(3, 2).Equals("00"))
            {
                ReturnStr = " And SubString(DepartmentID2,6,1)='" + DepartmentID.Substring(5, 1) + "' And SubString(DepartmentID2,5,1)='0' And Left(DepartmentID2,4)='" + DepartmentID.Substring(0, 4) + "'";
            }
            else
            {
                //如果属于支队司政后防某一单位
                if (DepartmentID.Substring(4, 1).Equals("0"))
                {
                    ReturnStr = " And Left(DepartmentID2,6)= '" + DepartmentID.Substring(0, 6) + "'";
                }

            }
        }

        return ReturnStr;

    }

    private string GetWhere3(string DepartmentID2)
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        DepartmentID = DepartmentID2.Substring(0, 4) + DepartmentID.Substring(4, 2) + "0";
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            ReturnStr = "And SubString(DepartmentID,6,1) IN('1','2','3','4') And  SubString(DepartmentID,5,1) ='0' And Left(DepartmentID,4)='" + DepartmentID.Substring(0, 4) + "'";
        }
        else
        {
            //总队人员
            if (DepartmentID.Substring(3, 2).Equals("00"))
            {
                ReturnStr = " And SubString(DepartmentID,6,1)='" + DepartmentID.Substring(5, 1) + "' And SubString(DepartmentID,5,1)='0' And Left(DepartmentID,4)='" + DepartmentID.Substring(0, 4) + "'";
            }
            else
            {
                //如果属于支队司政后防某一单位
                if (DepartmentID.Substring(4, 1).Equals("0"))
                {
                    ReturnStr = " And Left(DepartmentID,6)= '" + DepartmentID.Substring(0, 6) + "'";
                }

            }
        }

        return ReturnStr;

    }

    private string GetWhere2(string DepartmentID2)
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        DepartmentID = DepartmentID2.Substring(0, 4) + DepartmentID.Substring(4, 2) + "0";
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            ReturnStr = "And SubString(UserID,6,1) IN('1','2','3','4') And  SubString(UserID,5,1) ='0' And Left(UserID,4)='" + DepartmentID.Substring(0, 4) + "'";
        }
        else
        {
            //总队人员
            if (DepartmentID.Substring(3, 2).Equals("00"))
            {
                ReturnStr = " And SubString(UserID,6,1)='" + DepartmentID.Substring(5, 1) + "' And SubString(UserID,5,1)='0' And Left(UserID,4)='" + DepartmentID.Substring(0, 4) + "'";
            }
            else
            {
                //如果属于支队司政后防某一单位
                if (DepartmentID.Substring(4, 1).Equals("0"))
                {
                    ReturnStr = " And  left(userid,6)= '" + DepartmentID.Substring(0, 6) + "'";
                }

            }
        }

        return ReturnStr;
    }

    private int GetGroupByLen()
    {
        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        //是否是司政后防管理员
        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return 4;
        }
        else
        {
            return 6;
        }
    }


    private string GetDepartmentID()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "";
        }
        if (!DepartmentID.Substring(4, 1).Equals("0"))
            return "";

        return " Right(a.DepartmentID,2)=''" + DepartmentID.Substring(5, 1) + "0'' And ";

    }

    private string GetDepartmentID2()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "";
        }
        if (!DepartmentID.Substring(4, 1).Equals("0"))
            return "";

        return " Right(a.DepartmentID,2)='" + DepartmentID.Substring(5, 1) + "0' And ";

    }

    private string GetDepartmentIDChar()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "";
        }
        if (!DepartmentID.Substring(4, 1).Equals("0"))
            return "";

        return " Right(DepartmentID,2)='" + DepartmentID.Substring(5, 1) + "0' And ";

    }


    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();

    }
    private void BindStartMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            StartMonthList.Items.Add(li);
        }
        StartMonthList.SelectedIndex = StartMonthList.Items.IndexOf(StartMonthList.Items.FindByValue(DateTime.Now.Month.ToString()));
    }
    private void BindEndMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            EndMonthList.Items.Add(li);
        }
        EndMonthList.SelectedIndex = EndMonthList.Items.IndexOf(EndMonthList.Items.FindByValue(DateTime.Now.Month.ToString()));
    }

    private void CreateTable()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        //初始化各项分值
        StandardInfo standard = Standard.GetStandard();
        TotalScore = standard.TotalScore;
        TaskScore = standard.TotalScore * standard.TaskPer;
        BaseScore = standard.TotalScore * standard.BasePer;
        InspectScore = standard.TotalScore * standard.InspectPer;
        CheckScore = standard.TotalScore * standard.CheckPer;
        AwardScore = 40;
        int i = 1;


        DataView dv = SplitPage.ExecuteSQL("SELECT DepartmentID, AliasName FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'").Tables[0].DefaultView;
        foreach (DataRowView drv in dv)
        {
            decimal totalScore = 0;
            decimal tempScore = 0;
            TableRow tr = new TableRow();
            tr.CssClass = "grid_item";
            tr.HorizontalAlign = HorizontalAlign.Right;
            TableCell tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.Width = Unit.Pixel(30);
            tc.Text = i.ToString();
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = drv["AliasName"].ToString();
            tr.Cells.Add(tc);

            //工作任务得分
            tc = new TableCell();
            //总得分
            decimal ts = GetTaskScore(drv["DepartmentID"].ToString());
            //总分
            decimal tt = GetTaskTotalScore();
            tempScore = TaskScore * (ts / tt);
            totalScore += tempScore;
            string GS = "(" + ts + " / " + tt + ")*" + TaskScore.ToString("0") + "=" + totalScore.ToString("0.00") + "";
            tc.Text = "<a href='TaskMonthDetail.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + "&Departmentid=" + drv["DepartmentID"].ToString().Substring(0, 5) + DepartmentID.Substring(5, 1) + "0&GS=" + GS + "'>" + tempScore.ToString("0.00") + "</a>";
            tr.Cells.Add(tc);

            //基础数据得分
            tc = new TableCell();
            int monthcont = Convert.ToInt32(EndMonthList.SelectedValue) - Convert.ToInt32(StartMonthList.SelectedValue) + 1;
            decimal tol = GetBaseTotalScore();
            decimal so = GetBaseScore(drv["DepartmentID"].ToString());
            tempScore = (((tol * monthcont) - so) / (tol * monthcont)) * BaseScore;
            totalScore += tempScore;
            GS = "(((" + tol.ToString("0.00") + " * " + monthcont + ") -" + so.ToString("0.00") + ") / (" + tol.ToString("0.00") + " *" + monthcont + ")) *" + BaseScore.ToString("0.00") + "=" + tempScore.ToString("0.00");
            //tc.Text = tempScore.ToString("0.00");
            tc.Text = "<a href='BaseView3.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + "&Departmentid=" + drv["DepartmentID"].ToString().Substring(0, 5) + DepartmentID.Substring(5, 1) + "0&GS=" + GS + "'>" + tempScore.ToString("0.00") + "</a>";

            tr.Cells.Add(tc);

            //工作督察得分
            tc = new TableCell();
            //工作督察总分
            decimal its = GetInspectTotalScore();
            //工作督察得分
            decimal iss = GetInspectScore(drv["DepartmentID"].ToString());
            tempScore = ((its - iss) / its) * InspectScore;
            totalScore += tempScore;
            GS = "((" + its.ToString("0.00") + " - " + iss.ToString("0.00") + ") /" + its.ToString("0.00") + ") *" + InspectScore.ToString("0.00") + "=" + tempScore.ToString("0.00");
            tc.Text = "<a href='InspectDepartment.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + "&Departmentid=" + drv["DepartmentID"].ToString().Substring(0, 5) + DepartmentID.Substring(5, 1) + "0&GS=" + GS + "'>" + tempScore.ToString("0.00") + "</a>";

            //tc.Text = tempScore.ToString("0.00");
            tr.Cells.Add(tc);

            //考核验收得分
            tc = new TableCell();
            decimal cts = GetCheckTotalScore();
            decimal css = GetCheckScore(drv["DepartmentID"].ToString());
            tempScore = ((cts - css) / cts) * CheckScore;
            totalScore += tempScore;
            GS = "((" + cts + " -" + css + ") /" + cts + ") *" + CheckScore + "=" + tempScore.ToString("0.00");
            //tc.Text = tempScore.ToString("0.00");
            tc.Text = "<a href='CheckDepartment.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + "&Departmentid=" + drv["DepartmentID"].ToString().Substring(0, 5) + DepartmentID.Substring(5, 1) + "0&GS=" + GS + "'>" + tempScore.ToString("0.00") + "</a>";

            tr.Cells.Add(tc);

            //工作奖惩得分
            tc = new TableCell();
            tempScore = GetAwardScore(drv["DepartmentID"].ToString());
            totalScore += tempScore;
            GS = tempScore.ToString("0.00");
            tc.Text = "<a href='AwardDepartment.aspx?Year=" + YearList.SelectedValue + "&SM=" + StartMonthList.SelectedValue + "&EM=" + EndMonthList.SelectedValue + "&Departmentid=" + drv["DepartmentID"].ToString().Substring(0, 5) + DepartmentID.Substring(5, 1) + "0&GS=" + GS + "'>" + tempScore.ToString("0.00") + "</a>";

            // tc.Text = tempScore.ToString("0.00");
            tr.Cells.Add(tc);

            //综合得分
            tc = new TableCell();
            tc.Text = totalScore.ToString("0.00");
            tr.Cells.Add(tc);

            tb.Rows.Add(tr);

            ResultInfo result = new ResultInfo(drv["AliasName"].ToString(), Convert.ToDecimal(totalScore.ToString("0.00")));
            results.Add(result);

            i++;
        }
        BindChart();
    }
    private void BindChart()
    {
        Comparer c = new Comparer();
        results.Sort(c);
        UserChart.Titles["Title"].Text = YearList.SelectedItem.Text + "各单位综合得分情况统计表";
        UserChart.ChartAreas["Default"].AxisX.LabelsAutoFit = false;
        UserChart.Series["综合得分"].Points.DataBindXY(results, "DepartmentName", results, "Score");
    }
    //获取任务总分
    private decimal GetTaskTotalScore()
    {
        string Departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string sql = "select sum(initscore) from tasks where (left(CreateDepartment,6) = " + Departmentid.Substring(0, 6) + ") and year(enddate)=" + YearList.SelectedValue + " and month(enddate)>=" + StartMonthList.SelectedValue + " and month(enddate)<=" + EndMonthList.SelectedValue;
        DataView dv = SplitPage.ExecuteSQL(sql).Tables[0].DefaultView;
        if (dv[0][0] != DBNull.Value)
        {
            return Convert.ToDecimal(dv[0][0]);
        }
        else
        {
            return 1;
        }
    }
    //获取某单位任务实得分
    private decimal GetTaskScore(string departmentID)
    {
        string Departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        DataView dv = SplitPage.ExecuteSQL("select sum(score) from taskusers where taskid in (select taskid from tasks where year(enddate)=" + YearList.SelectedValue + " and month(enddate)>=" + StartMonthList.SelectedValue + " and month(enddate)<=" + EndMonthList.SelectedValue + " and (left(CreateDepartment,6) = " + Departmentid.Substring(0, 6) + ") )  " + GetWhere2(departmentID)).Tables[0].DefaultView;
        if (dv[0][0] != DBNull.Value)
        {
            return Convert.ToDecimal(dv[0][0]);
        }
        else
        {
            return 0;
        }
    }
    //获取基础数据总分
    private decimal GetBaseTotalScore()
    {
        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        string Where = string.Empty;
        if (ConstDepartmentID.Substring(4, 1).Equals("0"))
        {
            if (Flag)
            {
                Where = " Left(DepartmentID,4)='" + ConstDepartmentID.Substring(0, 4) + "'";
            }
            else
            {
                Where = " Right(DepartmentID,2)='" + ConstDepartmentID.Substring(5, 1) + "0'";
            }
        }
        DataView dv = SplitPage.ExecuteSQL("Select Sum(TotalScore) From Base Where " + Where + " ").Tables[0].DefaultView;
        if (dv == null || dv[0][0] == DBNull.Value)
        {
            return 1;
        }
        else
        {
            return decimal.Parse(dv[0][0].ToString());
        }

    }
    //获取某单位基础数据实得分
    private decimal GetBaseScore(string departmentID)
    {
        string sql = "select sum(itemscore) from baseitems where yearvalue=" + YearList.SelectedValue + " and monthvalue>=" + StartMonthList.SelectedValue + " and monthvalue<=" + EndMonthList.SelectedValue + " " + GetWhere3(departmentID);
        DataView dv = SplitPage.ExecuteSQL(sql).Tables[0].DefaultView;
        if (dv[0][0] != DBNull.Value)
        {
            return Convert.ToDecimal(dv[0][0]);
        }
        else
        {
            return 0;
        }
    }
    //获取工作督察总分
    private decimal GetInspectTotalScore()
    {
        string sql = "select sum(totalscore) from inspects where " + GetDepartmentIDChar() + " year(inspectdate)=" + YearList.SelectedValue + " and month(inspectdate)>=" + StartMonthList.SelectedValue + " and month(inspectdate)<=" + EndMonthList.SelectedValue;
        DataView dv = SplitPage.ExecuteSQL(sql).Tables[0].DefaultView;
        if (dv[0][0] != DBNull.Value)
        {
            return Convert.ToDecimal(dv[0][0]);
        }
        else
        {
            return 1;
        }
    }
    //获取某单位工作督察实得分
    private decimal GetInspectScore(string departmentID)
    {
        string sql = "select sum(itemscore) from inspectitems where inspectid in (select inspectid from inspects where year(inspectdate)=" + YearList.SelectedValue + " and month(inspectdate)>=" + StartMonthList.SelectedValue + " and month(inspectdate)<=" + EndMonthList.SelectedValue + ") " + GetWhere(departmentID);
        DataView dv = SplitPage.ExecuteSQL(sql).Tables[0].DefaultView;
        if (dv[0][0] != DBNull.Value)
        {
            return Convert.ToDecimal(dv[0][0]);
        }
        else
        {
            return 0;
        }
    }
    //获取考核验收总分
    private decimal GetCheckTotalScore()
    {
        string sql = "select sum(totalscore) from checks where " + GetDepartmentIDChar() + " year(checkdate)=" + YearList.SelectedValue + " and month(checkdate)>=" + StartMonthList.SelectedValue + " and month(checkdate)<=" + EndMonthList.SelectedValue;
        DataView dv = SplitPage.ExecuteSQL(sql).Tables[0].DefaultView;
        if (dv[0][0] != DBNull.Value)
        {
            return Convert.ToDecimal(dv[0][0]);
        }
        else
        {
            return 1;
        }
    }
    //获取某单位考核验收实得分
    private decimal GetCheckScore(string departmentID)
    {
        DataView dv = SplitPage.ExecuteSQL("select sum(itemscore) from checkitems where checkid in (select checkid from checks where " + GetDepartmentIDChar() + " year(checkdate)=" + YearList.SelectedValue + " and month(checkdate)>=" + StartMonthList.SelectedValue + " and month(checkdate)<=" + EndMonthList.SelectedValue + ") " + GetWhere(departmentID)).Tables[0].DefaultView;
        if (dv[0][0] != DBNull.Value)
        {
            return Convert.ToDecimal(dv[0][0]);
        }
        else
        {
            return 0;
        }
    }
    //获取某单位专项工作实得分
    private decimal GetAwardScore(string departmentID)
    {
        string sql = "select sum(itemscore) from awarditems where awardid in (select awardid from awards where " + GetDepartmentIDChar() + " year(awarddate)=" + YearList.SelectedValue + " and month(awarddate)>=" + StartMonthList.SelectedValue + " and month(awarddate)<=" + EndMonthList.SelectedValue + " ) " + GetWhere(departmentID);
        DataView dv = SplitPage.ExecuteSQL(sql).Tables[0].DefaultView;
        if (dv[0][0] != DBNull.Value)
        {
            return Convert.ToDecimal(dv[0][0]);
        }
        else
        {
            return 0;
        }
    }
    protected void ButtonEvaluate_Click(object sender, EventArgs e)
    {
        CreateTable();
    }
    protected void ButtonStandard_Click(object sender, EventArgs e)
    {
        Response.Redirect("AllStandard.aspx");
    }
}

